package com.dlc.felear.xiaoensale.customView;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.view.View;
import android.widget.TextView;

import com.dlc.felear.xiaoensale.R;
import com.dlc.felear.xiaoensale.constant.Constant;

public class DeviceStatusView implements Handler.Callback {
    private Context context;
    private View view;
    public static final int COLOR_NOR = 1;
    public static final int COLOR_TIPS = 4;
    public static final int COLOR_KX = 2;
    public static final int COLOR_ERR = 3;
    private TextView textView;
    private Handler handler;
    private static final int HANDLER_W1 = 123;
    private int lastStatus = 0;
    public static int ALARMTIME= Constant.BZZ_DELAY;

    public DeviceStatusView(final Context context, View view) {
        this.context = context;
        this.view = view;
        textView = view.findViewById(R.id.device_tv_status);
        handler = new Handler(this);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Settings.ACTION_SETTINGS);
                context.startActivity(intent);
            }
        });
    }

    public void setMaskColor(int type) {
        switch (type) {
            case COLOR_TIPS:
                view.setBackgroundResource(R.drawable.rl_device_status_normal_shape);
                textView.setText("放回菜夹显示实际价钱");
                lastStatus = type;
                break;
            case COLOR_NOR:
                view.setBackgroundResource(R.drawable.rl_device_status_normal_shape);
                textView.setText("请拿起菜夹取菜");
                lastStatus = type;
                break;
            case COLOR_KX:
                view.setBackgroundResource(R.drawable.rl_device_status_kx_shape);
                textView.setText("请把盘子放到指定区域");
                lastStatus = type;
                break;
            case COLOR_ERR:
                view.setBackgroundResource(R.drawable.rl_device_status_jg_shape);
                textView.setText("请放盘子或扫码后再夹菜");
                handler.sendEmptyMessageDelayed(HANDLER_W1,ALARMTIME);
                break;
            default:
        }
    }

    public void setTips(int weihgt){
        if(weihgt>0){
            setMaskColor(COLOR_TIPS);
        }else {
            setMaskColor(COLOR_NOR);
        }
    }


    @Override
    public boolean handleMessage(Message msg) {
        if (msg.what == HANDLER_W1) {
            if (lastStatus == COLOR_NOR) {
                view.setBackgroundResource(R.drawable.rl_device_status_normal_shape);
                textView.setText("请自由夹菜");
            } else {
                view.setBackgroundResource(R.drawable.rl_device_status_kx_shape);
                textView.setText("请把盘子放到指定区域");
            }
        }
        return false;
    }
}
